<template>
  <el-dialog
      v-model="dialogVisible"
      destroy-on-close
      title="匹配详情"
      width="80%"
      @close="handleClose"
  >
    <div v-if="detailData" v-loading="loading" class="match-detail-content">
      <el-descriptions :column="2" border title="单据信息">
        <el-descriptions-item label="匹配ID">{{ detailData.matchId || detailData.id || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="匹配状态">
          <el-tag :type="getTagType(detailData.matchStatus || detailData.matchResult)">
            {{ detailData.matchStatus || detailData.matchResult || '未知状态' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="订单编号">{{ detailData.orderNumber || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="订单金额">
          {{ formatCurrency(detailData.orderAmount) || '0.00' }}
        </el-descriptions-item>
        <el-descriptions-item label="收货单编号">{{ detailData.receiptNumber || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="收货单金额">
          {{ formatCurrency(detailData.receiptAmount) || '0.00' }}
        </el-descriptions-item>
        <el-descriptions-item label="发票编号">{{ detailData.invoiceNumber || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="发票金额">
          {{ formatCurrency(detailData.invoiceAmount) || '0.00' }}
        </el-descriptions-item>
        <el-descriptions-item label="供应商名称">{{ detailData.supplierName || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="匹配时间">{{
            detailData.matchTime || detailData.createTime || '暂无'
          }}
        </el-descriptions-item>
        <el-descriptions-item :span="2" label="差异信息">
          {{ detailData.differenceReason || detailData.difference || '无差异' }}
        </el-descriptions-item>
        <el-descriptions-item :span="2" label="备注">
          {{ detailData.remark || '无备注' }}
        </el-descriptions-item>
      </el-descriptions>

      <div v-if="detailData.relatedDocuments && detailData.relatedDocuments.length > 0" class="match-related-documents">
        <h3>关联单据信息</h3>
        <el-table :data="detailData.relatedDocuments" border style="width: 100%; margin-bottom: 20px">
          <el-table-column label="序号" type="index" width="60"></el-table-column>
          <el-table-column label="单据类型" prop="type" width="120">
            <template #default="scope">
              <el-tag :type="getDocTypeTag(scope.row.type)">{{ scope.row.type }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="单据编号" min-width="180" prop="number"></el-table-column>
          <el-table-column label="金额" prop="amount" width="150">
            <template #default="scope">
              {{ formatCurrency(scope.row.amount) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" width="120">
            <template #default="scope">
              <el-tag :type="getMatchStatusTag(scope.row.status)">{{ scope.row.status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="匹配时间" prop="matchTime" width="180"></el-table-column>
        </el-table>
      </div>

      <div v-if="detailData.items && detailData.items.length > 0" class="match-items">
        <h3>匹配明细</h3>
        <el-table :data="detailData.items" border style="width: 100%">
          <el-table-column label="序号" type="index" width="60"></el-table-column>
          <el-table-column label="商品名称" min-width="180" prop="goodsName"></el-table-column>
          <el-table-column label="规格型号" min-width="150" prop="specification"></el-table-column>
          <el-table-column label="单位" prop="unit" width="80"></el-table-column>
          <el-table-column label="数量" prop="quantity" width="100"></el-table-column>
          <el-table-column label="单价" prop="price" width="120">
            <template #default="scope">
              {{ formatCurrency(scope.row.price) }}
            </template>
          </el-table-column>
          <el-table-column label="金额" prop="amount" width="150">
            <template #default="scope">
              {{ formatCurrency(scope.row.amount) }}
            </template>
          </el-table-column>
          <el-table-column label="匹配状态" prop="matchStatus" width="120">
            <template #default="scope">
              <el-tag :type="getMatchStatusTag(scope.row.matchStatus)">{{ scope.row.matchStatus }}</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div v-else class="no-data">
      <el-empty description="暂无详情数据"></el-empty>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import {defineEmits, defineProps, ref, watch} from 'vue';
import {ElMessage} from 'element-plus';
import {MatchService} from '@/api/match-service';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  matchId: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['update:visible', 'close']);

const dialogVisible = ref(props.visible);
const loading = ref(false);
const detailData = ref<any>(null);

// 监听visible属性变化
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal;
  if (newVal && props.matchId) {
    getMatchDetail();
  }
});

// 监听dialogVisible变化，同步到父组件
watch(() => dialogVisible.value, (newVal) => {
  emit('update:visible', newVal);
  if (!newVal) {
    emit('close');
  }
});

// 获取匹配详情
const getMatchDetail = async () => {
  if (!props.matchId) return;

  loading.value = true;
  try {
    const res = await MatchService.getMatchDetail(props.matchId);
    if (res.code === 200) {
      detailData.value = res.data;
    } else {
      ElMessage.error(res.message || '获取匹配详情失败');
    }
  } catch (error) {
    console.error('获取匹配详情失败', error);
    ElMessage.error('获取匹配详情失败');
  } finally {
    loading.value = false;
  }
};

// 格式化货币
const formatCurrency = (value: number | undefined) => {
  if (!value && value !== 0) return '0.00';
  return Number(value).toFixed(2);
};

// 获取标签类型
const getTagType = (status: string | undefined) => {
  if (!status) return '';

  switch (status) {
    case '匹配成功':
    case '匹配':
      return 'success';
    case '部分匹配':
      return 'warning';
    case '待匹配':
      return 'info';
    case '匹配异常':
    case '不匹配':
      return 'danger';
    default:
      return '';
  }
};

// 获取单据类型标签
const getDocTypeTag = (type: string) => {
  switch (type) {
    case '订单':
      return 'primary';
    case '收货单':
      return 'success';
    case '发票':
      return 'warning';
    default:
      return 'info';
  }
};

// 获取匹配状态标签
const getMatchStatusTag = (status: string) => {
  switch (status) {
    case '匹配成功':
    case '匹配':
      return 'success';
    case '部分匹配':
      return 'warning';
    case '待匹配':
      return 'info';
    case '匹配异常':
    case '不匹配':
      return 'danger';
    default:
      return 'info';
  }
};

// 关闭对话框
const handleClose = () => {
  dialogVisible.value = false;
  detailData.value = null;
};
</script>

<style scoped>
.match-detail-content {
  padding: 20px 0;
}

.match-related-documents,
.match-items {
  margin-top: 30px;
}

.match-related-documents h3,
.match-items h3 {
  margin-bottom: 15px;
  font-size: 16px;
  font-weight: bold;
}

.no-data {
  padding: 40px 0;
}
</style> 